package com.itheima.controller.pms;

import com.itheima.core.PageInfo;
import com.itheima.core.R;
import com.itheima.pojo.pms.PmsProduct;
import com.itheima.pojo.pms.vo.CreatePmsProductVo;
import com.itheima.pojo.pms.vo.SelectPmsProductVo;
import com.itheima.pojo.pms.vo.UpdateStatusVo;
import com.itheima.service.pms.*;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/product")
public class PmsProductController {

    @DubboReference
    private PmsProductService pmsProductService;

    /**
     * 创建商品
     * @param pmsProductVo  接收数据实体类
     * @return
     */
    @PostMapping("/create")
    public R createProduct(@RequestBody CreatePmsProductVo pmsProductVo){
        pmsProductService.saveProduct(pmsProductVo);
        return R.success();
    }

    /**
     * 查询商品
     * @return
     */
    @GetMapping("/list")
    public R selectProduct(@RequestParam(required = false) Integer brandId, @RequestParam(required = false) String  keyword, @RequestParam(required = false) Integer pageNum, @RequestParam(required = false) Integer  pageSize,
                           @RequestParam(required = false) Integer productCategoryId, @RequestParam(required = false) String productSn, @RequestParam(required = false) Integer publishStatus, @RequestParam(required = false) Integer verifyStatus){
        SelectPmsProductVo selectPmsProductVo = new SelectPmsProductVo();
        selectPmsProductVo.setBrandId(brandId);
        selectPmsProductVo.setKeyword(keyword);
        selectPmsProductVo.setPageNum(pageNum);
        selectPmsProductVo.setPageSize(pageSize);
        selectPmsProductVo.setProductCategoryId(productCategoryId);
        selectPmsProductVo.setProductSn(productSn);
        selectPmsProductVo.setPublishStatus(publishStatus);
        selectPmsProductVo.setVerifyStatus(verifyStatus);
        PageInfo pageInfo = pmsProductService.selectProduct(selectPmsProductVo);
        return R.success(pageInfo);
    }

    /**
     * 根据商品名称或货号模糊查询
     * @param keyword
     * @return
     */
    @GetMapping("/simpleList")
    public R likeSelect(@RequestParam String keyword){
        List<PmsProduct> pmsProductList = pmsProductService.likeSelect(keyword);
        return R.success(pmsProductList);
    }

    /**
     * 批量修改删除状态
     * @return
     */
    @PostMapping("/update/deleteStatus")
    public R updateDeleteStatus(@RequestParam Integer deleteStatus, @RequestParam List<Integer> ids){
        pmsProductService.updateDeleteStatus(ids,deleteStatus);
        return R.success();
    }

    /**
     * 批量设为新品
     * @param
     * @return
     */
    @PostMapping("/update/newStatus")
    public R updateNewStatus(@RequestParam Integer newStatus, @RequestParam List<Integer> ids){
        pmsProductService.updateNewStatus(ids, newStatus);
        return R.success();
    }

    /**
     *批量上下架商品
     * @param
     * @return
     */
    @PostMapping("/update/publishStatus")
    public R updatePublishStatus(@RequestParam Integer publishStatus, @RequestParam List<Integer> ids){
        pmsProductService.updatePublishStatus(ids,publishStatus);
        return R.success();
    }

    /**
     *批量推荐商品
     * @param
     * @return
     */
    @PostMapping("/update/recommendStatus")
    public R updateRecommendStatus(@RequestParam Integer recommendStatus, @RequestParam List<Integer> ids){
        pmsProductService.updateRecommendStatus(ids, recommendStatus);
        return R.success();
    }

    /**
     *批量修改审核状态
     * @param
     * @return
     */
    @PostMapping("/update/verifyStatus")
    public R updateVerifyStatus(@RequestParam String detail, @RequestParam List<Integer> ids, Integer verifyStatus){
        pmsProductService.updateVerifyStatus(ids, verifyStatus, detail );
        return R.success();
    }

    /**
     * 更新商品
     * @param productVo
     * @param id
     * @return
     */
    @PostMapping("/update/{id}")
    public R updateProduct(@RequestBody CreatePmsProductVo productVo,@PathVariable Integer id){
        pmsProductService.updateProduct(productVo,id);
        return R.success();
    }

    /**
     * 根据商品id获取商品编辑信息
     * @param id
     * @return
     */
    @GetMapping("/updateInfo/{id}")
    public R getProductById(@PathVariable Integer id){
        CreatePmsProductVo pmsProductVo = pmsProductService.getProductById(id);
        return R.success(pmsProductVo);
    }
}
